var vue = new Vue({
    el : '#app',
    data : {
        treeData : [],
        treeProps: {
            children: 'children',
            label: 'menuName'
        },
        tableData: [],
        loading: false,

        formVisible: false,
        formRead: false,
        menuParentId : 0,
        menuForm: {
            menuName : '',
            menuCode : '',
            url : '',
            menuOrder:'',
            remark : '',
            parentId : 0,
            onlyForSuper:0,
        },
        formRules: {
            menuName: [{required: true, message: '请输入菜单名称'}],
            menuCode: [{required: true, message: '请输入菜单编码'}],
            url: [{required: true, message: '请输入链接地址'}],
        },
    },
    created : function(){
        this.loadAllMenu();
        this.loadBranchMenu(0);
    },
    methods : {
        loadAllMenu: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/menu/list' ,
                success: function (result) {
                    if (result.code === 200) {
                        self.treeData = result.body;
                    }
                }
            })
        },
        loadBranchMenu: function (menuId) {
            this.menuParentId = menuId;
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/menu/branch/'+ this.menuParentId,
                success: function (result) {
                    if (result.code === 200) {
                        self.tableData = result.body;
                    }
                }
            })
        },
        save : function(){
            var self = this;

            self.$refs['menuFormRef'].validate(function (valid) {
                if (!valid) return false;

                self.menuForm.parentId = self.menuParentId;
                $.ajax({
                    url: constant.ROOT_SITE + '/system/menu/save',
                    data: JSON.stringify(self.menuForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.formVisible = false;
                            self.loadAllMenu();
                            self.loadBranchMenu(self.menuParentId)
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },
        //删除
        deleteMenu : function(menu){
            var self = this;

            this.$confirm('您确定用删除该菜单吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:  constant.ROOT_SITE + '/system/menu/delete/' + menu.id,
                    success: function (result) {
                        if (result.code === 200 && result.body) {
                            self.$message({
                                type: 'success',
                                message: '删除成功！'
                            });
                            self.loadAllMenu();
                            self.loadBranchMenu(self.menuParentId)
                        }
                    }
                })
            });

        },
        updateBusinessStatus:function(row){
            var self = this;
            var form = {
                id : row.id,
                businessFlag:!row.businessFlag,
            }
            $.ajax({
                url: constant.ROOT_SITE + '/system/menu/save',
                data: JSON.stringify(form),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '操作成功！'
                        });
                        row.businessFlag = !row.businessFlag;
                    } else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },
        showEditDialog : function(menu){
            if(menu) $.extend(this.menuForm, menu);
            this.formVisible = true;
        },
        handleNodeClick : function(menu){
            this.loadBranchMenu(menu.id);
        },
        handleDialogClose :function () {
            this.menuForm={
                menuName : '',
                menuCode : '',
                url : '',
                remark : '',
                parentId : 0,
                menuOrder:'',
                onlyForSuper:0,
            };
        }
    }
});